package com.msmall.community.bbs.dao.hibernate;

import java.util.List;

import com.msmall.base.dao.hibernate.BaseDaoHibernate;
import com.msmall.base.exception.DataNotFoundException;
import com.msmall.community.bbs.dao.FavoriteDao;
import com.msmall.community.bbs.model.Favorite;


public class FavoriteDaoHibernate extends BaseDaoHibernate<Favorite, Long> implements FavoriteDao{

	public FavoriteDaoHibernate(){
		super(Favorite.class);
	}
	public Favorite getFavorite(Long userid, Long topicId) throws DataNotFoundException {
		try{
			String queryString = " from Favorite t where t.userid = ? and t.topicid=?";
			Object[] objs = new Object[2];
			objs[0] = userid;
			objs[1] = topicId;
			List list = super.getHibernateTemplate().find(queryString, objs);
			if(null != list && list.size() > 0)
				return (Favorite) list.get(0);
			return null;
		}catch(Exception e){
			throw new DataNotFoundException(e);
		}
	}
	
	/**
	 * 获得用户所有收藏的帖子
	 */
	public List<Favorite> getAllByUserid(Long userid) throws Exception{
		try{
			String queryString = " from Favorite t where t.userid = ? order by createTime desc";
			List list = super.getHibernateTemplate().find(queryString, new Object[]{userid});
			return list;
		}catch(Exception e){
			throw e;
		}
	}
}
